package com.xiaoma.verbal.web.admin;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xiaoma.tpo.utils.Pager;
import com.xiaoma.verbal.domain.QuQuestion;
import com.xiaoma.verbal.service.QuestionService;
import com.xiaoma.verbal.utils.RequestUtils;

@Controller
@RequestMapping("/test/experience")
public class QuExperienceManagementAct {
	@Autowired
	private QuestionService questionService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(Integer id, Integer questionType, String qnum, String tagText, String content, Integer page, Integer rows,
			HttpServletRequest request, ModelMap model) {
		// 当前页
		Map<String, Object> result = new HashMap<String, Object>();
		tagText = RequestUtils.getQueryParam(request, "tagText");
		content = RequestUtils.getQueryParam(request, "content");
		Pager pager = this.questionService.list(id, questionType, qnum, tagText, content, page, rows);
		result.put("rows", pager.getList());
		result.put("total", pager.getTotalCount());
		return result;

	}

	@RequestMapping(value = "/import", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> importData(@RequestParam(value = "excelData", required = false) MultipartFile file, String fileType, HttpServletRequest request, ModelMap model) {
		// 当前页
		Map<String, Object> result = new HashMap<String, Object>();

		InputStream fis = null;
		int status = 1;
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try {
			fis = file.getInputStream();
			int version = 2007;
			if (fileType != null && fileType.length() > 0 && fileType.equals("xls")) {
				version = 2003;
			}
			status = questionService.importData(version, fis);
		} catch (Exception e) {
			e.printStackTrace();
			status = 3;
			json.put("msg", e.getMessage());
		}
		result.put("result", status);

		return result;

	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Integer save(Integer qid, String questionType, String questionNum, String content, String qtags, String translation, HttpServletRequest request, ModelMap model) {
		Integer result = 1;
		// 当前页
		try {
			questionService.save(qid, questionType, questionNum, content, qtags, translation);

		} catch (Exception e) {
			result = 0;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Integer update(Integer qid, String questionType, String questionNum, String content, String qtags, String translation, HttpServletRequest request, ModelMap model) {
		Integer result = 1;
		// 当前页
		try {
			questionService.update(qid, questionType, questionNum, content, qtags, translation);

		} catch (Exception e) {
			result = 0;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}
	
	@RequestMapping(value = "/checkQnumIsExist", method = RequestMethod.GET)
	@ResponseBody
	public Integer checkQnumIsExist(String qnum,HttpServletRequest request, ModelMap model) {
		Integer result = 0;
		// 当前页
		try {
			return questionService.checkQnumIsExist(qnum.trim());
		} catch (Exception e) {
			result = 1;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}
	

}
